/**
 * @file lavas config
 * @author *__ author __*{% if: *__ email __* %}(*__ email __*){% /if %}
 */

const path = require('path');

const BUILD_PATH = path.resolve(__dirname, 'dist');
const isDev = process.env.NODE_ENV === 'development';
const isProd = process.env.NODE_ENV === 'production';
function resolve(dir) {
  return path.join(__dirname, dir)
}
module.exports = {
  build: {
    ssr: false,
    path: BUILD_PATH,
    publicPath: '/',
    ssrCopy: isDev ? [] : [
      {
        src: 'server.prod.js',
      },
      {
        src: 'package.json',
      },
    ],
    extend(config, { type, env }) {
      /* if (type === 'client' && env === 'development') {
                config.devServer = {
                    proxy: {
                        '/api': {
                            target: 'http://114.84.44.28:9090',
                            pathRewrite: {'^/api' : ''},
                            secure: false,
                            changeOrigin: true
                        }
                    }
                };
            } */
      if (type === 'base') {
        const { rules } = config.module;
        rules.push({
          test: /\.tsx?$/,
          use: [
            {
              loader: 'tslint-loader',
              options: {
                configFile: path.resolve(__dirname, './tslint.json'),
                appendTsSuffixTo: [/\.vue$/],
              },
            },
          ],
          exclude: /node_modules/,
        },
        {
          test: /\.tsx?$/,
          use: [
            {
              loader: 'ts-loader',
              options: {
                // 指定特定的ts编译配置，为了区分脚本的ts配置
                configFile: path.resolve(__dirname, './tsconfig.json'),
                appendTsSuffixTo: [/\.vue$/],
              },
            },
          ],
          exclude: /node_modules/,
        });

        /* console.log(rules[0].use[0]);

                rules[0].use[0].options.loaders.ts = [
                    {
                        loader: 'ts-loader',
                        options: {
                            appendTsSuffixTo: [/\.vue$/]
                        }
                    }
                ] */
      }
    },
    extendWithWebpackChain(config, { type, env }) {
      config.module.rule('vue').use('vue').tap((options) => {
        options.loaders.ts = [
          {
            loader: 'ts-loader',
            options: {
              appendTsSuffixTo: [/\.vue$/],
            },
          },
        ];
        return options;
      });


      config.module.rule('img')
      .exclude.add(resolve('assets/icons'))

  // 2.添加一个规则icons,没有及添加
  config.module.rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('assets/icons')).end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({symbolId: 'icon-[name]'})

    },
  },
  router: {
    mode: 'history',
    base: '/',
    pageTransition: {
      type: 'fade',
      transitionClass: 'fade',
    },
  },
  serviceWorker: {
    // If true, generate service-worker.js and sw-register.js
    // Default to `false`
    enable: true,

    // Path of service worker template
    swSrc: path.join(__dirname, 'core/service-worker.js'),

    // Path of generated service worker file
    swDest: path.join(BUILD_PATH, 'service-worker.js'),

    // If true, `workbox.routing.registerNavigationRoute()` won't be generated
    // Defaults to `false`
    disableGenerateNavigationRoute: true,

    // URL of appshell page
    // ONLY works in SSR mode
    appshellUrl: '/appshell',
  },
  middleware: {
    all: [], // 同时运行在客户端和服务端
    client: ['first'], // 仅客户端
    server: [], // 仅服务端
  },
};
